package br.ufc.great.dtx.server;

import java.io.IOException;

import javax.swing.UIManager;

import br.ufc.great.dtx.dm.DataManager;
import br.ufc.great.dtx.dm.DataManagerSednaImpl;
import br.ufc.great.dtx.server.config.Config;
import br.ufc.great.dtx.server.console.DTXConsole;
import br.ufc.great.dtx.tm.TransactionManager;
import br.ufc.great.xml.XMLDocument;

/**
 * 
 * @author Leonardo Oliveira Moreira
 *
 * Classe utilizada para inicializar a aplica��o
 */
public final class DTXStart {

	/* Serviços */
	private static DataManager dataManager = null;
	private static TransactionManager transactionManager = null;
	
	private static DTXConsole console = null;
	private static DTXOutput output = null;
	
	private static DTXServer server;
	
	/**
	 * Método estático para inicialização dos serviços globais
	 */
	static {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
        	e.printStackTrace();
        	System.exit(0);
        }
		
        console = new DTXConsole();
        console.build();
		
		output = console.getOutput();
	}
	
	public static boolean startDataManager() {
		try {
			dataManager = new DataManagerSednaImpl(output);
			dataManager.start(Config.DATABASE_NAME);
			XMLDocument document = null;
			if (dataManager.connect(Config.DATABASE_NAME, Config.DATABASE_URL, Config.DATABASE_USER, Config.DATABASE_PASSWORD)) {
				document = dataManager.loadDocument(Config.DATABASE_DOCUMENT);
				dataManager.setXMLDocument(document);
				dataManager.close();
			}
			output.print("DTX", "DataManager (" + Config.DATABASE_DOCUMENT + ") started", DTXOutput.MESSAGE);
			//System.out.println("DataManager (" + Config.DATABASE_DOCUMENT + ") started");
		}
        catch (IOException e) {
			output.print(e.getMessage(), DTXOutput.ERROR);
			System.out.println(e.getMessage());
			output.exit();
        }
        return true;
	}
	
	public static boolean startTransactionManager() {
		transactionManager = new TransactionManager(output, dataManager);
		while (! transactionManager.isRunning());
		output.print("DTX", "Transaction Manager started", DTXOutput.MESSAGE);
		//System.out.println("Transaction Manager started");
		return true;
	}
	
	public static boolean startDTXServer() {
		server = new DTXServer(output);
		server.start();
		return true;
	}

	public static boolean stopDataManager() {
		if (dataManager != null)
			try {
				output.print("Stoping DataManager...", DTXOutput.MESSAGE);
				//System.out.println("Stoping DataManager...");
				synchronized (dataManager) {
					dataManager.stop(Config.DATABASE_NAME);						
				}
			}
			catch (IOException e) {
				System.out.println(e.getMessage());
				output.print(e.getMessage(), DTXOutput.ERROR);
			}
		return true;
	}
	
	public static boolean stopTransactionManager() {
		if (transactionManager != null)
			transactionManager.end();
		output.print("DTX", "Transaction Manager stoped", DTXOutput.MESSAGE);
		//System.out.println("Transaction Manager stoped");
		return true;
	}
	
	public static boolean stopDTXServer() {
		if (server != null)
			server.end();
		output.print("DTXServer", "DTXServer stoped", DTXOutput.MESSAGE);
		//System.out.println("DTXServer stoped");
		return true;
	}

	
	/**
	 * Método que inicializa a aplicação
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		console.setVisible(true);
	}
	
	/**
	 * Método para recuperar a referência para o gerenciador de dados
	 * 
	 * @return
	 */
	public static final DataManager getDataManager() {
		return dataManager;
	}

	/**
	 * Método para recuperar a referência para o gerenciador de transação
	 * 
	 * @return
	 */
	public static final TransactionManager getTransactionManager() {
		return transactionManager;
	}
	
}
